/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.commons.event;

import com.android1.amarena2d.nodes.behavior.LifecyleTick;

public class Event extends BaseEvent implements LifecyleTick {

    public static final int TYPE = 0;

    private int type;
    private float arg1;
    private float arg2;
    private Object object;

    boolean recycleable = false;
    Event next;
    private static final Object poolSync = new Object();
    private static Event pool;

    public Event(int type) {
        this.type = type;
    }

    public Event() {
        this.type = TYPE;
    }

    public void fire() {
        engine.getEventBus().fire(this);
    }

    @Override
    public void tick(float d) {
        fire();
    }

    public static void createPool(int size) {
        Event e = null;
        for (int i = 0; i < size; i++) {
            Event ne = new Event();
            ne.recycleable = true;
            ne.next = e;
            e = ne;
        }
        pool = e;
    }

    public static Event obtain(int type, Object o) {
        Event e = obtain(type);
        e.object = o;
        return e;
    }

    public static Event obtain(int type, float arg1) {
        Event e = obtain(type);
        e.arg1 = arg1;
        return e;
    }

    public static Event obtain(int type, float arg1, float arg2) {
        Event e = obtain(type);
        e.arg1 = arg1;
        e.arg2 = arg2;
        return e;
    }


    public static Event obtain(int type) {
        synchronized (poolSync) {
            if (pool != null) {
                Event m = pool;
                pool = m.next;
                m.next = null;
                m.type = type;
                return m;
            }
        }
        Event e = new Event(type);
        e.recycleable = true;
        return e;
    }

    public void recycle() {
        synchronized (poolSync) {
            clear();
            next = pool;
            pool = this;
        }
    }

    public void clear() {
        this.type = 0;
        this.arg1 = 0;
        this.arg2 = 0;
        this.object = null;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    @Override
    public boolean isRecyclable() {
        return recycleable;
    }

    public float getArg1() {
        return arg1;
    }

    public void setArg1(int arg1) {
        this.arg1 = arg1;
    }

    public float getArg2() {
        return arg2;
    }

    public void setArg2(int arg2) {
        this.arg2 = arg2;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }


}